package com.ruoyi.web.api;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ItemNotFoundException;
import com.ruoyi.common.utils.ObjectUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.exam.serive.IScoresService;
import com.ruoyi.exam.vo.ExamPaperVO;
import com.ruoyi.exam.vo.ExamRealVO;
import com.ruoyi.questionBank.domain.ExamPaper;
import com.ruoyi.questionBank.domain.Question;
import com.ruoyi.questionBank.service.IExamPaperService;
import com.ruoyi.questionBank.service.IQuestionService;
import com.ruoyi.questionBank.vo.CourseQuestionVO;
import com.ruoyi.questionBank.vo.QuestionListVO;
import com.ruoyi.student.impl.StudentUserService;
import com.ruoyi.student.vo.UserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 考核
 */
@RestController
@RequestMapping("/api/exam")
public class ExamApi extends BaseController {

    @Autowired
    private IQuestionService questionService;
    @Autowired
    private IExamPaperService paperService;
    @Autowired
    private IScoresService scoresService;

    /**
     * 理论考核
     * @return
     */
    @RequestMapping("/process")
    public AjaxResult process()
    {
        ExamPaper examPaper = paperService.get(1);
        if (examPaper == null) { //不存在，插入一个id为1的试卷
            examPaper = new ExamPaper();
            examPaper.setId(1);
            examPaper.setCreateTime(new Date());
            examPaper.setName("统一考试试卷");
            examPaper.setTheoryStatus(2);
            examPaper.setUpdateTime(examPaper.getUpdateTime());
            try {
                paperService.addDefault(examPaper); //存在了也不管
            } catch (Exception e) {
            }

        }
//        SELECT * FROM sys_exam_paper WHERE theory_status = 2 order by create_time desc limit 1
//        ExamPaper examPaper = paperService.getTheoryProcess();
//        if (examPaper == null || examPaper.getId() == null || examPaper.getId() <= 0) {
//            throw new ItemNotFoundException("没有进行中的考核");
//        }
//        if (CollectionUtils.isEmpty(examPaper.getQuestionIdList()) || StrUtil.isEmpty(examPaper.getQuestionIds())) {
//            throw new ItemNotFoundException("试卷未添加试题");
//        }
//        List<Question> list = questionService.getEnable(examPaper.getQuestionIdList());
        //随机组卷
        List<Question> list = questionService.listRandomEnable();
        if(list == null || list.size() <= 0) {
            throw new ItemNotFoundException("没有试题");
        }
        //生成一张临时试卷
        LoginUser loginUser = SecurityUtils.getLoginUser();
        ExamPaper tmpExamPaper = new ExamPaper();
        tmpExamPaper.setDefault();
        tmpExamPaper.setCreateTime(new Date());
        tmpExamPaper.setRemark("用户"+loginUser.getUserId()+"的试卷");
        tmpExamPaper.setTheoryStatus(3); //已结束
        tmpExamPaper.setUpdateTime(examPaper.getUpdateTime());
        tmpExamPaper.setQuestionDataList(list);
        try {
            paperService.addDefault(tmpExamPaper); //存在了也不管
        } catch (Exception e) {
            return AjaxResult.error("生成考题失败");
        }
        //
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("paper",tmpExamPaper);
        resultMap.put("questions",list);
        return AjaxResult.success(resultMap);
    }

    /**
     * 提交答案
     * @return
     */
    @RequestMapping("/submit")
    public AjaxResult submit(@RequestBody ExamPaperVO examPaperVO)
    {
        ObjectUtil.checkNull(examPaperVO.getPaperId() ,"试卷ID为空");
        ObjectUtil.checkNull(examPaperVO.getAnswers() ,"答案为空");
        Map<String,String> map = questionService.submitAnswer(examPaperVO);
        return AjaxResult.success(map);
    }

    /**
     * 上报实操考核成绩
     * @return
     */
    @RequestMapping("/real")
    public AjaxResult real(@RequestBody ExamRealVO examPaperVO)
    {
        ObjectUtil.checkNull(examPaperVO.getScores() ,"成绩为空");
        scoresService.real(examPaperVO);
        return AjaxResult.success();
    }

}
